package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.entity.GoodsSpu;
import com.micro.modual.extension.GoodsSpuExtension;
import com.micro.modual.mapper.GoodsSpuMapper;
import com.micro.modual.model.bo.goodsspu.GoodsSpuBO;
import com.micro.modual.model.dto.goodsspu.GoodsSpuListDTO;
import com.micro.modual.service.GoodsSpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
*  <h1>商品spu表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("goodsSpuService")
public class GoodsSpuServiceImpl extends AbstractBasicsServiceImpl<GoodsSpu,Long> implements GoodsSpuService {

    @Autowired
    private GoodsSpuMapper goodsSpuMapper;

    /**
     * <h2>查找商品spu表 列表</h2>
     * @param params 列表查询请求对象
     * @return List<GoodsSpuListDTO> {@link GoodsSpuListDTO}s  商品spu表 列表返回对象
     */
    @Override
    public RespJsonPageData<GoodsSpuListDTO> findGoodsSpuList(Map<String, Object> params) {
        PageQuery pageQuery = new PageQuery(params);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<Map<String,Object>> goodsSpuList = goodsSpuMapper.findGoodsList(params);
        List<GoodsSpuListDTO> goodsSpuDTOList = JacksonUtil.getList(JacksonUtil.toJson(goodsSpuList),GoodsSpuListDTO.class);
        return new RespJsonPageData<GoodsSpuListDTO>(goodsSpuDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>新增商品spu表详情</h2>
     * @param goodsSpuBO {@link GoodsSpuBO} 商品spu表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addGoodsSpu(GoodsSpuBO goodsSpuBO) {
        GoodsSpu goodsSpu = GoodsSpuExtension.converAddGoodsSpu(goodsSpuBO);
        goodsSpuMapper.insertSelective(goodsSpu);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改商品spu表详情</h2>
     * @param goodsSpuBO  {@link GoodsSpuBO} 商品spu表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editGoodsSpu(GoodsSpuBO goodsSpuBO) {
        GoodsSpu goodsSpu = GoodsSpuExtension.converEditGoodsSpu(goodsSpuBO);
        goodsSpuMapper.updateByPrimaryKeySelective(goodsSpu);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除商品spu表信息</h2>
     * @param id  商品spu表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteGoodsSpu(Long id) {
        goodsSpuMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

}
